//
//  SetView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2024/12/26.
//

import SwiftUI
import Kingfisher

struct AiCustomView: View {
    @EnvironmentObject var themeManager: ThemeManager
    @StateObject var viewModel = AiCustomViewModel()
    @State var delTips = false
    @State private var showFullScreen = false //预览图片
    @State private var selectedIndex = 0

    var body: some View {
        ZStack{
            ScrollView(.vertical) {
                AiCustomItemView(title: "日记回复", key: IAiModelType.diaryReply.rawValue)
                Spacer(minLength: 15)
                AiCustomItemView(title: "聊天", key: IAiModelType.chat.rawValue)
                Spacer(minLength: 15)
                AiCustomItemView(title: "AI日记", key: IAiModelType.aiDiary.rawValue)
                Spacer(minLength: 15)
                AiCustomItemView(title: "每周来信", key: IAiModelType.weeklyLetter.rawValue)
                Spacer(minLength: 30)
                Button(action: {
                    SwiftEntryKitWrapper.showBottomPopup { dismiss in
                        AiCustomTutorialDialog() { tutorial in
                            dismiss()
                            if !userManager.isCustom() {
                                showToast("该功能需要定制会员才能使用")
                                return
                            }
                            SwiftEntryKitWrapper.showBottomPopup { dismiss in
                                AiCustomTutorialDialog2(bean: tutorial ?? AiTutorialChildBean(), onDismiss1: { tutorial in
                                    showTipsDialog(text: "已复制\(tutorial?.title ?? "")API 地址")
                                    copyClipboard(tutorial?.api)
                                    viewModel.mediaItems = [MediaItem(type: MediaType.networkImage(tutorial?.tutorial ?? "", tutorial?.tutorial ?? "", false))]
                                    showFullScreen = true
                                }, onDismiss2: { tutorial in
                                    showTipsDialog(text: "已复制\(tutorial?.title ?? "")API 地址")
                                    copyClipboard(tutorial?.api)
                                    //进入网页配置
                                    guard let url = URL(string: tutorial?.web ?? ""),
                                          UIApplication.shared.canOpenURL(url)
                                    else { return }
                                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                                })
                            }
                        }
                    }
                }) {
                    Text("教程")
                        .foregroundColor(themeManager.themeColor)
                        .font(.system(size: 16))
                        .padding(.horizontal,10)
                        .padding(.vertical,15)
                        .frame(width: 300)
                    
                }.background(themeManager.themeColor.opacity(0.1))
                    .cornerRadius(8)
            }.navigationTitle("自定义API")
                .frame(maxWidth: .infinity)
                .padding().background(.bg)
              
        }.onDisappear{
            viewModel.cancelRequest()
        }.fullScreenCover(isPresented: $showFullScreen) {
            FullScreenMediaView(mediaItems: viewModel.mediaItems, currentIndex: $selectedIndex)
        }
    }
}

class AiCustomViewModel : BaseViewModel {
    @Published var mediaItems: [MediaItem] = []
}
//#Preview {
//    SetView()
//}


//定义预览提供者
struct AiCustom_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: AiCustomView())
    }
}
